namespace SharpTalk.Core.Base
{
    using System;
    using Exceptions;
    using Interfaces;
    using Enums;
    using System.Runtime.Serialization;

    [DataContract]
    [Serializable]
    public abstract class ChatRoomBase 
        : PropertyChangedBase, IChatRoom, IEquatable<ChatRoomBase>
    {
        private Guid _guid;
        private byte[] _image;
        private string _name;
        private ChatRoomTypeEnum _type = ChatRoomTypeEnum.NotDefined;
        private byte[] _passwordHash;
        private Guid _owner;
        private string _motd;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> class.
        /// </summary>
        protected internal ChatRoomBase()
        {
            _guid = Guid.NewGuid();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> class.
        /// </summary>
        protected internal ChatRoomBase(Guid guid)
        {
            _guid = guid;
        }
        /// <summary>
        /// Obtains the Guid
        /// </summary>
        [DataMember(IsRequired = true)]
        public Guid Guid
        {
            get { return _guid; }
            set { _guid = value; }
        }
        /// <summary>
        /// Obtain or define the image
        /// </summary>
        [DataMember]
        public virtual byte[] Image
        {
            get { return _image; }
            set
            {
                _image = value;
                NotifyPropertyChange(()=>Image);
            }
        }
        /// <summary>
        /// The name of the chatroom
        /// </summary>
        [DataMember(IsRequired = true)]
        public virtual string Name
        {
            get { return _name; }
            set
            {
                if(String.IsNullOrEmpty(value))throw new ChatRoomNameException();

                _name = value;
                NotifyPropertyChange(()=>Name);
            }
        }
        /// <summary>
        /// Obtains or defines the chatroom type
        /// </summary>
        [DataMember(IsRequired = true)]
        public virtual ChatRoomTypeEnum Type
        {
            get { return _type; }
            set
            {
                if (value == ChatRoomTypeEnum.NotDefined)
                    throw new ChatRoomTypeNotDefinedException();

                _type = value;
                NotifyPropertyChange(() => Type);
            }
        }
        /// <summary>
        /// Obtain or define the password hash
        /// </summary>
        [DataMember]
        public virtual byte[] PasswordHash
        {
            get { return _passwordHash; }
            set
            {
                if(IsDefault && value !=null)
                    throw new ChatRoomException(Resources.Messages.default_chatroom_cannot_have_password);

                _passwordHash = value;
                NotifyPropertyChange(() => PasswordHash);

                if (value == null && Type.HasFlag(ChatRoomTypeEnum.Protected))
                    Type ^= ChatRoomTypeEnum.Protected; // Removes the protected flag
                else
                {
                    if(!Type.HasFlag(ChatRoomTypeEnum.Protected) && value != null)
                        Type |= ChatRoomTypeEnum.Protected; // Adds the protected flag
                }
            }
        }

        /// <summary>
        /// Determines if the instance is the default public chatroom. 
        /// </summary>
        [DataMember]
        public virtual bool IsDefault
        {
            get; set;
        }
        /// <summary>
        /// Obtains or define the owner
        /// </summary>
        [DataMember(IsRequired = true)]
        public virtual Guid Owner
        {
            get { return _owner; }
            set
            {
                _owner = value;
                NotifyPropertyChange(() => Owner);
            }
        }
        /// <summary>
        /// Obtanis or defines the Message of the day
        /// </summary>
        [DataMember(IsRequired = false)]
        public virtual string Motd
        {
            get { return _motd; }
            set { _motd = value; NotifyPropertyChange(()=>Motd); }
        }
        /// <summary>
        /// Indicates whether the current <see cref="T:SharpTalk.Core.Interfaces.IChatRoom"/> is equal to another <see cref="T:SharpTalk.Core.Interfaces.IChatRoom"/> of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(IChatRoom other)
        {
            return !ReferenceEquals(null, other) 
                && Guid.Equals(other.Guid);
        }
        /// <summary>
        /// Indicates whether the current <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> is equal to another <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/>.
        /// </summary>
        /// <returns>
        /// true if the current <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(ChatRoomBase other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other._guid.Equals(_guid);
        }
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (ChatRoomBase)) return false;
            return Equals((ChatRoomBase) obj);
        }
        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return _guid.GetHashCode();
        }
        /// <summary>
        /// Indicates whether the current <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> is equal to another <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/>.
        /// </summary>
        /// <param name="left">An existing instance of the <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> class.</param>
        /// <param name="right">An existing instance of the <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> class.</param>
        /// <returns>True if equals otherwise False.</returns>
        public static bool operator ==(ChatRoomBase left, ChatRoomBase right)
        {
            return Equals(left, right);
        }
        /// <summary>
        /// Indicates whether the current <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> is equal to another <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/>.
        /// </summary>
        /// <param name="left">An existing instance of the <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> class.</param>
        /// <param name="right">An existing instance of the <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> class.</param>
        /// <returns>True if equals otherwise False.</returns>
        public static bool operator !=(ChatRoomBase left, ChatRoomBase right)
        {
            return !Equals(left, right);
        }
        
    }
}